"use strict";
/* Controllers */
app.controller("detectionRuleAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'detectionRuleAddService', function ($scope, $location, $rootScope, $routeParams, detectionRuleAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.id = $routeParams.id; // 如果ID!=0为更新
    var errorFn = function (result) {
        alert(result);
    };

    $scope.setDetectionRule = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id,
            ruleCode: vm.p.ruleCode,
            detectName: vm.p.detectName,
            detectPolicy: vm.p.detectPolicy,
            dataSource: vm.p.dataSource,
            ownerId: vm.p.ownerId
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //window.location = "#/star/detectionRule/detectionRule_list";
                alert("执行成功！")
            } else {
                alert(response.msg);
            }
        };
        detectionRuleAddService.setDetectionRule(params, procFn, errorFn);
    };
    $scope.getDetectionRule = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //vm.p = JSON.stringify(response.data);
                vm.p = response.data;
            } else {
                alert(response.msg);
            }
        };
        detectionRuleAddService.getDetectionRule(params, procFn, errorFn);
    };

    //
    if(vm.p.id!=0) {
        $scope.getDetectionRule();
    }
}]);


/* Services */
app.service('detectionRuleAddService', ['ajaxService', function (ajaxService) {

    this.setDetectionRule = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/detection_rule/setDetectionRule.do", successFunction, errorFunction);
    };
    this.getDetectionRule = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/detection_rule/getDetectionRule.do", successFunction, errorFunction);
    };
}]);

